$.validator.setDefaults({
    submitHandler: function() {
      	var params = $(".form-div").serialize(); 
      	$.ajax( {  
                type : "POST",  
                url : serverIP+"/user/goods/save",  
                data : params,  
                xhrFields: {
	                withCredentials: true    // 设置sessionId相同
	            },
                success : function(data) {
                	if(data.code==0){
                		alert("新增物料登记成功");
                	}else{
                		alert(data.msg);
                	}
                }  
        }); 
    }
});

$(document).ready(function() {
	$('.form-div').validate({
		rules: {
		 	name: "required",
		 	brand: "required",
		 	model: "required",
		 	unit: "required",
		 	format: "required"
		},
		messages: {
	      	name: "请输入商品名称",
	      	brand: "请输入商品品牌",
	      	model: "请输入商品型号",
	      	unit: "请输入计量单位(个/件等)",
	      	format: "请输入商品规格"
		}	
	})
	
})
//	获取商品品牌
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"brand"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="brand"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})

$(function(){
	if($(".admin").height()<700){
		$('.nav-btn').height('50px').find('img').hide();
	}else{
		$('.nav-btn').height('150px').find('img').show();
	}
//	获取商品型号
	var brand=$("select[name='brand']").val();
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetModelList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data : {
        	brand:brand
        },
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$("select[name='model']").append('<option value='+obj.value  +'>'+obj.name+'</option>');
			})
		}
	})
$("select[name='brand']").change(function(){
		$('.model').show();
		$("select[name='model']").html("");
		var brand=$("select[name='brand']").val();
		$.ajax({
			type:"get",
			url:serverIP+"/user/util/GetModelList",
			dataType:"json",
			xhrFields: {
	            withCredentials: true    // 设置sessionId相同
	        },
	        data : {
	        	brand:brand
	        },
			success:function (data) {
				$.each(data.data, function(index,obj) {
					$("select[name='model']").append('<option value='+obj.value  +'>'+obj.name+'</option>');
				})
			}
		})
	})
//	获取计量单位
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"unit"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="unit"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
//	获取颜色
	$.ajax({
		type:"get",
		url:serverIP+"/user/util/GetCodeList",
		dataType:"json",
		xhrFields: {
            withCredentials: true    // 设置sessionId相同
        },
        data:{codeType:"color"},
		success:function (data) {
			$.each(data.data, function(index,obj) {
				$('select[name="color"]').append('<option value='+obj.value+'>'+obj.name+'</option>');
			})
		}
	})
})
//监听浏览器窗口变化
window.onresize = function(){
	var bodyH=$("body").height();
	$(".leftbar").height(bodyH-45+"px");
	$(".content").height(bodyH-45+"px");
	$(".admin").height($(".content").height()-45+"px");
	if($(".admin").height()<600){
		$('.nav-btn').height('50px').find('img').hide();
	}else{
		$('.nav-btn').height('150px').find('img').show();
	}
}
$(".icon-ok").click(function() {
	$(".submit").click();
})
$(".back").click(function() {
	history.back();
});